package android_board;

//import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android_board.GUIBoard.FONTSIZE;

import common.GUIColor;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android_codebehind.AndroidComponentFactory;
import dk.dtu.compute.course02312.matadorfinal.MainActivity;
import dk.dtu.compute.course02312.matadorfinal.R;

final public class GUICenter {
	private static GUICenter center;
	private static LinearLayout centerContent;
	private static RelativeLayout centerPanel;
	public static TextView[] textViews = new TextView[7];
	private static ImageView centerDefault = new ImageView(MainActivity.context);
	public static ImageView divider;
	public static ImageView[] cars = new ImageView[GUIBoard.MAX_PLAYER_COUNT];
	private String chanceCardText = "Prøv Lykken";
	private RelativeLayout carsPane = new RelativeLayout(MainActivity.context);

	private GUICenter() {
		int panelHeight = GUIBoard.FIELDHEIGHT * 3;
		int panelWidth = GUIBoard.FIELDWIDTH * 3;
		centerPanel = new RelativeLayout(MainActivity.context);
		centerContent = new LinearLayout(MainActivity.context);
		centerContent.setOrientation(LinearLayout.VERTICAL);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				panelWidth, panelHeight);
		// centerPanel.addMouseListener(new CenterMouseListener(this));
		// Add an empty textView to maintain size of empty cells
		TextView tv = new TextView(MainActivity.context);
		tv.setId(GUIBoard.generateViewId());
		// tv.setText("(4-6,4-6)");
		tv.setTag("cellsize");
		tv.setHeight(panelHeight);
		tv.setWidth(panelWidth);
		tv.setGravity(Gravity.CENTER);
		centerPanel.addView(tv);
		centerPanel.setBackgroundColor(GUIBoard.BASECOLOR.getRGB());
		AndroidComponentFactory.createLineBorder(centerPanel, 4,
				GUIColor.BLACK.getRGB());

		for (int i = 0; i < textViews.length; i++) {
			TextView tv2 = new TextView(MainActivity.context);
			tv2.setId(GUIBoard.generateViewId());
			tv2.setGravity(Gravity.CENTER);
			textViews[i] = tv2;
			centerContent.addView(textViews[i]);
			if (i == 0) {
				tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP, FONTSIZE + 16);

				divider = new ImageView(MainActivity.context);
				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, 2);
				int margin = 4; 
				lp.setMargins(margin, 0, margin, 0);
				divider.setLayoutParams(lp);
				
				divider.setBackgroundColor(GUIColor.BLACK.getRGB());
				divider.setVisibility(View.GONE);
				GUICenter.centerContent.addView(divider);
			}
		}
		centerPanel.addView(centerContent, params);
		for (int i = 0; i < cars.length; i++) {
			ImageView iv = new ImageView(MainActivity.context);
			cars[i] = iv;
			RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
					WRAP_CONTENT, WRAP_CONTENT);
			Bitmap carBitmap = BitmapFactory.decodeResource(
					MainActivity.context.getResources(), R.drawable.cars);
			int right = 0, bottom = 0;
			int top = (3 * GUIBoard.FIELDHEIGHT) - carBitmap.getHeight() - 10;
			int left = (27 * i) + 10;
			p.setMargins(left, top, right, bottom);
			// iv.setBounds(25 * i + 3, 0, Player.ICON_WIDTH,
			// Player.ICON_HEIGHT);
			// this.carsPane.setLayer(l, i + 6);
			// iv.setVisibility(View.GONE);
			GUICenter.centerPanel.addView(iv, p);
		}
		centerPanel.addView(this.carsPane);

		centerPanel.setOnClickListener(new OnClickListener() {
			@SuppressWarnings("unused")
			@Override
			public void onClick(View v) {
				MainActivity.context.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						displayChanceCard();
					}
				});
			}
		});

		displayDefault();
	}

	/**
	 * Retrieves the singleton instance
	 * 
	 * @return the Center instance
	 */
	public static GUICenter getInstance() {
		if (center == null) {
			center = new GUICenter();
		}
		return center;
	}

	/**
	 * Clears all center labels; (icon label is handled seperatly)
	 */
	public void clearCenter() {
		AndroidComponentFactory.createLineBorder(GUICenter.centerPanel, 4,
				GUIColor.BLACK.getRGB());
		for (TextView tv : textViews) {
			tv.setText("");
			tv.setCompoundDrawables(null, null, null, null);
			tv.setVisibility(View.GONE);
		}
		for (int i = 0; i < GUIBoard.MAX_PLAYER_COUNT; i++) {
			GUICenter.cars[i].setVisibility(View.GONE);
		}
		GUICenter.centerDefault.setVisibility(View.GONE);
		GUICenter.divider.setVisibility(View.GONE);
	}

	/**
	 * Sets the backgroundcolor of the center
	 * 
	 * @param bgColor
	 *            The background color
	 */
	public void setBGColor(GUIColor bgColor) {
		centerPanel.setBackgroundColor(bgColor.getRGB());
	}

	/**
	 * Sets the foregroundcolor of the center
	 * 
	 * @param fgColor
	 *            The foreground color
	 */
	public void setFGColor(GUIColor fgColor) {
		for (int i = 0; i < textViews.length; i++) {
			textViews[i].setTextColor(fgColor.getRGB());
		}
	}

	/**
	 * Display standard version of the center
	 */
	public void displayDefault() {
		clearCenter();
		Drawable d = MainActivity.context.getResources().getDrawable(
				R.drawable.center_proev_lykken);

		centerPanel.setBackgroundColor(MainActivity.context.getResources()
				.getColor(R.color.basecolor));

		int height = d.getIntrinsicHeight();
		int width = d.getIntrinsicWidth();
		d.setBounds(new Rect(0, 0, width, height));
		centerDefault.setImageDrawable(d);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				width, height);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		if (GUICenter.centerPanel.getParent() == null)
			GUICenter.centerPanel.addView(centerDefault, params);
		else
			GUICenter.centerDefault.setVisibility(View.VISIBLE);
	}

	/**
	 * Displays the next chance card
	 */
	public void displayChanceCard() {
		clearCenter();
		centerPanel.setBackgroundColor(GUIColor.WHITE.getRGB());
		textViews[1].setText(this.chanceCardText);
		textViews[1].setVisibility(View.VISIBLE);
	}

	/**
	 * Set the next chance card
	 * 
	 * @param txt
	 *            the text to display on the center.
	 */
	public void setChanceCard(String txt) {
		this.chanceCardText = txt;
	}

	public RelativeLayout getCenterPanel() {
		return centerPanel;
	}
}